package com.itheima.Semaphore;

import java.util.concurrent.Semaphore;

public class MySemaphoreDemo {
    public static void main(String[] args) {
        MyRunnable mr=new MyRunnable();
        for (int i = 0; i < 100; i++) {
                new Thread(mr).start();
        }
    }
}

class MyRunnable implements Runnable{
    //获得管理员对象
    //表示未来这个线程任务同时只能由2个线程去并发执行
private Semaphore semaphore=new Semaphore(2);
    @Override
    public void run() {
        //获得通行证
        try {

            semaphore.acquire();        //获得锁
            //开始行驶
            System.out.println("获得通行证");
            Thread.sleep(2000);
            System.out.println("归还通行证");
            semaphore.release();        //归还锁
        } catch (InterruptedException e) {
            e.printStackTrace();
        }


    }
}